You can check page here: http://jsfiddle.net/7JhjN/

Basicly, there is a hell of duplication going on and I don't like this. I am sure there is something wrong since it's not a correct programming logic.

How can I shorten this function?

The system works like this: 1. There are 6x select boxes. (option values are the same) 2. There are 6x divs for output. 3. Whenever a select box value gets changed, image in target div gets changed.

有帮助吗?

解决方案

how about this:

HTML:

<select class='item_early' name="item_early1" data-id="1"><option value="0"></option></select>
<select class='item_early' name="item_early2" data-id="2"> <option value="0"></option></select>
<select class='item_early' name="item_early3" data-id="3"> <option value="0"></option></select>
<select class='item_early' name="item_early4" data-id="4"> <option value="0"></option></select>

<div id="result_item_early1"></div>
<div id="result_item_early2"></div>
<div id="result_item_early3"></div>
<div id="result_item_early4"></div>

Javascript:

$('.item_early').change( function() {
  var value = $(this).val();
  var id = $(this).data('id');

  $('#result_item_early'+id).html('<img src="http://www.sobafire.com/themes/default/images/loading.gif">');
  $('#result_item_early'+id).load('http://www.sobafire.com/ajax.php?ajaxType=ITEM&itemID=' + value);
});

其他提示

Your problem, you have something that looks like this:

$('#item_early1').change( function() {
    var value = $(this).val();
    //directly set loading value as html
    $('#result_item_early1').html('<img src="http://www.sobafire.com/themes/default/images/loading.gif">');

    //change image url source from server (expected return value is something like <img src="" />
    $('#result_item_early1').load('http://www.sobafire.com/ajax.php?ajaxType=ITEM&itemID=' + value);

});

And you repeat for the different constants.

The answer is replace the constants with variable names like this then put it in a function:

function doit(itemID,resultID) {
  $(itemID).change( function() {
      var value = $(this).val();
      //directly set loading value as html
      $(resultID).html('<img src="http://www.sobafire.com/themes/default/images/loading.gif">');

      //change image url source from server (expected return value is something like <img src="" />
      $(resultID).load('http://www.sobafire.com/ajax.php?ajaxType=ITEM&itemID=' + value);
  });
}

Then call that function for all your constants:

doit('#item_early1','#result_item_early1');
doit('#item_early2','#result_item_early2');
doit('#item_early3','#result_item_early3');
doit('#item_early4','#result_item_early4');
doit('#item_early5','#result_item_early5');
doit('#item_early6','#result_item_early6');

You can simplify your code by using starts with attribute selector.

$('select[name^="item_early"]').change( function() {
    var $this = $(this);
    var value = $this.val();
    var $resultDiv = $('#result_' + $this.attr('name'));
    //directly set loading value as html
    $resultDiv.html('<img src="http://www.sobafire.com/themes/default/images/loading.gif">');

    //change image url source from server (expected return value is something like <img src="" />
    $resultDiv.load('http://www.sobafire.com/ajax.php?ajaxType=ITEM&itemID=' + value);

});

Working Demo

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top