我有一个脚本,它产生的id看起来像数组元素,如下所示:
<select name="serviceTypeID[1]" id="serviceTypeID[1]" ...
<select name="serviceTypeID[2]" id="serviceTypeID[2]" ...
<select name="serviceTypeID[3]" id="serviceTypeID[3]" ...
<select name="serviceTypeID[4]" id="serviceTypeID[4]" ...
and so on up to 10 currently...
如何编写我的jquery脚本来引用它们?此代码不起作用。
$('#serviceTypeID[1],#serviceTypeID[2],#serviceTypeID[3],#serviceTypeID[4]').change(function() {
var first = parseInt( $('#firstService[1]').val() );
var second = parseInt( $('#secondService[1]').val() );
var third = parseInt( $('#thirdService[1]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal[1]').val( ( first + second + third + ' Total') );
first = parseInt( $('#firstService[2]').val() );
second = parseInt( $('#secondService[2]').val() );
third = parseInt( $('#thirdService[2]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal[2]').val( ( first + second + third + ' Total') );
first = parseInt( $('#firstService[3]').val() );
second = parseInt( $('#secondService[3]').val() );
third = parseInt( $('#thirdService[3]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal[3]').val( ( first + second + third + ' Total') );
first = parseInt( $('#firstService[4]').val() );
second = parseInt( $('#secondService[4]').val() );
third = parseInt( $('#thirdService[4]').val() );
if (isNaN(first)) first = 0;
if (isNaN(second)) second = 0;
if (isNaN(third)) third = 0;
$('#serviceTotal[4]').val( ( first + second + third + ' Total') );
//and so on up to 10 currently
});
没有什么从jquery脚本返回,所以我觉得我在我的jquery中引用它是错误的。感谢
答案 0 :(得分:0)
我会将div或其他元素包装在所有这些元素周围并使用事件委托http://api.jquery.com/on/
例如
说你把这些div包裹起来并给那个div一个id&#34; foo&#34;。 $(&#39;#FOO&#39)。在(&#39;变更&#39;&#39;选择&#39),函数(){ 的console.log(本); });
答案 1 :(得分:0)
你应该从他们不可思议的id中删除方括号......
<select name="serviceTypeID[1]" id="serviceTypeID1" ...
<select name="serviceTypeID[2]" id="serviceTypeID2" ...
<select name="serviceTypeID[3]" id="serviceTypeID3" ...
<select name="serviceTypeID[4]" id="serviceTypeID4" ...