Changing Img Src based on value entered in a Textf

2019-08-01 10:40发布

I need help creating a form with the following

  1. TEXTFIELD(will be used to enter 7digit model numbers)
  2. An image placeholder (will change the image placeholder's src based on a url for example it will become src="http://yourwebsite.com/product/TEXTFIELD.jpg)

  3. I need to somehow get the the H1 tag values from within the product's url

#3 IS STILL UNSOLVED !

I'm REALLY desperate for any type of help. I've googled the entire day REALLY need assistance. Thank You !

I have some code below that helps visualize what I'm kinda looking for. Please contact me if you need clarification or if I'm a bit confusing.

<form name="form1" method="post" action="">
       <p>7-digit product#
         <input type="text" name="model" id="model">
       </p>
       <p>
         <input name="start" type="hidden" id="start" value="http://www.mywebsite.com/Products/">
       </p>
       <p>
         <input name="end" type="hidden" id="end" value=".jpg">
       </p>
       <p><img name="proudctimage" src="=#start#model#end" width="32" height="32" alt=""></p>
     </form>

<script>
    var model_input = document.getElementById('model');
    var start = document.getElementById('start');
    var end = document.getElementById('end');
    var image = document.getElementsByTagName('img');

    model_input.onkeyup = function(e){
       image[0].src = start.value + model_input.value + end.value;
    }
</script>

~EDITED 9:00AM 5/29/12~


  1. The Values entered in the textfield gets deleted if you hit enter
  2. I need a way to grab a product's description stored in a H1 tag using the respective URL (The URL is the model number of what is entered in the textfield but uses a slightly different url structure that is different than the one used to grab images , see below ... http://mywebsite.com/ProductDetails.aspx?productid=TEXTFIELD) ***I Should make note that the URL used to get the H1 data will be a "cross domain" & not necessarily on the some domain. I read that jquery may not make requests on cross domains

3条回答
贼婆χ
2楼-- · 2019-08-01 11:05

In your html take a hidden div. suppose

<div id="loadSource"></div>

   $('#model').blur(function () {
        var modelVal = $.trim(this.value),
            startVal = $.trim($('#start').val()),
            endVal = $.trim($('#end').val());
        if( modelVal &&  startVal && endVal) {
           var imgUrl = startVal + modelVal + endVal,
               siteUrl = startVal + modelVal;
           $('img[name="productimage"]')
            .attr('src', );

           // process to get h1 tag

           $('#loadSource').load(''+ siteUrl +' h1', function() {
                  console.log( $('#loadSource h1').text() );
           });
        }
    });
查看更多
迷人小祖宗
3楼-- · 2019-08-01 11:08

If your using jquery, you could do this:

$(function () {  
    $('#model').change(function () {
        $('img[name="productimage"]').attr('src', $('#start').val() + $('#model').val() + $('#end').val());
    });
});
查看更多
戒情不戒烟
4楼-- · 2019-08-01 11:12

You could use Jquery to do this easily.

Grab a copy of Jquery from http://www.jquery.com or use the CDN version by pasting this into your head section:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

Here's a simplified version:

If the start and end parts of your URL are not going to change you could simplify your form:

​<form>
    <label>Model Num</label>
    <input type="text" name="model" id="model" />
    <input type="button" value="Update Image" id="update" />
</form>
<img src="" />

​ Then With the following Jquery code, you can detect a click on the update button, grab the code from the text box and change the image src attribute to

http://mysite.com/products/typed_code_here

Just paste the jquery code (and the script tags) into your head section

<script>
    ​$(document).on('click','#update',function(){
        $('img').attr('src','http://mysite.com/product/'+$('#model').val()+'.jpg');
    });​​​​​​
</script>

If you want to do this without Jquery and if your html has to remain as above, you could use the following along with your original html (watch out for spelling mistakes in your code):

<script>
    var model_input = document.getElementById('model');
    var start = document.getElementById('start');
    var end = document.getElementById('end');
    var image = document.getElementsByTagName('img');

    model_input.onkeyup = function(e){
       image[0].src = start.value + model_input.value + end.value;
    }
</script>

The onkeyup event could be changed to blur, change etc depending on how you want to update the image. I'd suggest a button such that the user can update the image when they believe the code is valid.

Update

The following github project has made progress on the front of cross domain html requests with jQuery. https://github.com/padolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

Basically you'd just need to grab the product page via ajax (jQuery.ajax()) and then in the ajax callback you'd have to scan the returned HTML for the h1 element. Ultimately cross domain ajax is a design pattern and there are best practices associated with it. Grabbing the whole HTML page for the sake of an h1 element is not very effective. Consider revising your approach and be sure to check any copyright laws/terms and conditions if the site you're referencing is not your own.

查看更多
登录 后发表回答