2011-11-15 36 views
3
<select id="paymenttype"> 
<option value="">---Select---</option> 
<option>100</option> 
<option>200</option> 
<option>300</option> 
<option>400</option> 
<option>500</option> 
</select> 
<input type="text" id="check" value="" readonly/> 
<input type="text" id="update" value="" /> 

JSjQuery的:应当这里给出什么功能[更改或其他功能]

$("#paymenttype").change(function(){ 
$("#check").val($('#paymenttype option:selected').text()); 
}); 

$("#check").change(function(){ 
alert("soul"); 
$("#update").val(0); 
}); 

Jsfiddle code example

如上代码$("#check").change(function()这是行不通的显示?我应该使用什么函数才能让Jquery工作?当输入文本框中有更改时,我将添加?这只是一个例子

+0

只要元素失去焦点,'change'就会启动。 – Tomalak

+0

@JamesAllardice当输入文本框中有更改时,我将添加?这只是一个例子 – Ghostman

+1

@Tomalak - 我认为问题是绑定到'#check'的'change'事件从不会触发(并且它永远不会触发,因为元素是'readonly'),所以Richard D的答案是正确的。 –

回答

4

你只需要当你将该值设置为手动触发更改事件:

$("#paymenttype").change(function(){ 
    $("#check").val($('#paymenttype option:selected').text()).change(); // <-- HERE 
}); 

http://jsfiddle.net/infernalbadger/e6yEd/7/

你可以添加一个小插件,您可以设置值和火在一次通话中同时发生更改事件。

这样,你只需要调用$("#check").changeVal($('#paymenttype option:selected').text());

(function($) { 
    $.fn.changeVal = function(value) { 
     return this.val(value).change();  
    }; 
})(jQuery); 

http://jsfiddle.net/infernalbadger/e6yEd/11/

+0

打我吧lol :) – Val

1

尝试添加触发器

$("#paymenttype").change(function(){ 
     $("#check").val($('#paymenttype option:selected').text()).change(); 
    }); 

注意.change()在第二行的末尾