函数在jquery中不起作用

时间:2015-07-20 08:11:59

标签: jquery

<input id="CHAMPARAN EAST" 
       name="CHAMPARAN EAST" 
       onclick="SearchDistict('CHAMPARAN EAST');" type="checkbox" value="34">

我的功能

function SearchDistict(obj) {
    debugger;
    if (obj.checked) {
        districtCollection.push(obj.value);
    } else {
        districtCollection.pop(obj.value);
        //districtCollection.splice($.inArray(obj.value, districtCollection),1);

    }
    var postData = preparingSearch();
    debugger;
    postData.StateList = '';
    postData.DistrictList = districtCollection;
    SearchEngine();
    //CallSearchBorrower(postData);
}

当我们调用SearchDistict('CHAMPARAN EAST')时它的工作但不调用if语句

2 个答案:

答案 0 :(得分:1)

而不是传递复选框传递this对象的id,如下所示。 this对象将具有复选框的所有状态,但仅传递id值将无法解决您的目的。

 function SearchDistict(obj) 
{
        alert(obj.checked);
        if (obj.checked) 
         {
          districtCollection.push(obj.value);
         }
        else {
            districtCollection.pop(obj.value);
            //districtCollection.splice($.inArray(obj.value, districtCollection),1);

        }
        var postData = preparingSearch();
        debugger;
        postData.StateList = '';
        postData.DistrictList = districtCollection;
        SearchEngine();
        //CallSearchBorrower(postData);
    }
<input id="CHAMPARAN EAST" 
           name="CHAMPARAN EAST" 
           onclick="SearchDistict(this);" type="checkbox" value="34">

上面的代码将为您提供函数内部obj.checked的值

答案 1 :(得分:0)

obj是一个字符串,而不是复选框,因此它没有checked属性,而是可以使用this关键字传递对象引用本身行事件处理程序。

<input id="CHAMPARAN EAST" name="CHAMPARAN EAST" onclick="SearchDistict(this);" type="checkbox" value="34">

然后

function SearchDistict(obj) {
    debugger;
    if (obj.checked) {
        districtCollection.push(obj.value);
    } else {
        districtCollection.pop(obj.value);
        //districtCollection.splice($.inArray(obj.value, districtCollection),1);

    }
    var postData = preparingSearch();
    debugger;
    postData.StateList = '';
    postData.DistrictList = districtCollection;
    SearchEngine();
    //CallSearchBorrower(postData);
}