2011-12-07 17 views
1

编辑的JavaScript得到多个属性:一次平变化

随着我才知道这个名字是不是div标签有效的属性响应的提示,但它的选择。

function modifica(estado,numero){ 
    alert(numero); 
    $.ajax({ 
    type: "GET", 
    datatype: "html", 
    url: "icallverifica.php", 
    data: { 
    estado : estado, 
    modifica : "1" , 
    numero : numero 
    }, 
    success: function(data) { 
     alert(data); 
    } 
    }); 
} 

所以,我想在使用一次电平变化来获得多重属性:

if($valor == 0){ 
    mysql_select_db("iloja_icall", $con); 
    $result1 = mysql_query("SELECT * FROM icall WHERE verifica='0'"); 
    while($row = mysql_fetch_array($result1)){ 
    echo $row['numero'].'<select id="optin" name="'.$row['numero'].'" onchange="modifica(this.value,this.name)"> 
    <option value="chamada">Chamada</option> 
    <option value="atendeu">Atendeu</option> 
    <option value="atendeunao">Nao atendeu</option> 
    <option value="engano">Engano</option> 
</select>'; 
    echo '<br>'; 
    } 
} 

这个组合现在工作完美:d THANK YOU SO MUCH家伙,这就是为什么我爱计算器:)

不应该工作吗?

非常感谢!

+1

为什么不直接使用'的onchange =“modifica(本)”'那么你就可以得到所有这是从你的'modifica内性能'功能?或者只是传递'select'的'id'并使用javascript来获得你所需要的? –

+0

你能告诉我们这个与PHP代码,因为我们不知道从哪里来:$ row ['numero']' – noob

+0

“modifica(this.value,this.name)”是不正确的,这将返回选择哪个没有“name”属性,你想要做的就是使用“modifica(this)”,然后调用$(this).parent()。attr(“name”)来获得父类的名称选择 – ComputerSaysNo

回答

2

基于这个问题的标签,我看到你正在使用jQuery。

// assuming you're passing "this" to function 
function modifica(me) { 
    var id = me.parentNode.id; // or $(me).parent().attr('id'); etc 
} 

此外,你可以只取出onchange属性和与偶数监听器(推荐)设置:

$('#optin').change(function(e) { 
    // code here 
    var option = $(this).val(); // current selected option for example 

    // if you want to get ALL attributes of an element: 
    var attribs = $(this)[0].attributes; // or $('#elementID')... etc 
}); 

如果在所有元素的的希望迭代(环)属性,然后看看这个:How to iterate through all attributes in an HTML element?

0

'name'是div标签的无效属性。应该切换到'id'。这也消除了需要调用.attr(“名”),而使用:

$(this).parent().id;