简体   繁体   English

如何从具有相同ID的动态创建的元素中选择.val()或div .text()?

[英]How to get either select .val() or div .text() from dynamically created elements with same id?

Depending on the user type, my page dynamically creates either a select element (for admins to change) or a div with text (for regular users) using the same id. 根据用户类型,我的页面会动态创建一个select元素(供管理员更改)或一个具有相同ID的div文本(供普通用户使用)。

if ($user_type == 'admin') {
    echo "<tr><td>Type:</td><td><select id='type' >";
    echo "<option value='student' >student</option><option value='teacher' >teacher</option>";
    echo "</select></td></tr>";
}
else echo "<tr><td>Type:</td><td><div id='type'>" . $user_type . "</div></td></tr>";

When the page submits, I need either the .val() from the select element or the .text() from the div element. 页面提交时,我需要select元素中的.val()或div元素中的.text()。

I can't use .val() on the div element and I can't use .text() on the select element. 我不能在div元素上使用.val(),也不能在select元素上使用.text()。

Is there a way in jQuery / javascript to get one or the other, depending on which element type was created? jQuery / javascript中是否有一种方法可以获取一个或另一个,这取决于创建的元素类型?

make the else statement as so (use input[type=hidden], to use the .val()) 进行else语句(使用input [type = hidden]来使用.val())

else echo
"<tr>
  <td>Type:</td>
  <td>
    <!-- div to show the value -->
    <div>$user_type</div>
    <!-- hidden input type to get the value via jQuery .val() -->
    <input type='hidden' id='type' value='$user_type'>
  </td>
</tr>";

Oh by the way, you can use PHP variables inside strings that are defined with double quotes echo "$someVar"; 哦,顺便说一句,您可以在用双引号定义的字符串中使用PHP变量echo "$someVar";

Since you are printing out from PHP the HTML out put, by the same time you can print a Javascript variable who has what method use to get the value/text. 由于您是从PHP中打印出HTML输出,因此您可以同时打印一个Javascript变量,该变量具有使用哪种方法来获取值/文本。 Then, use the variable in your Javascript to perform one query or other. 然后,使用Javascript中的变量执行一个查询或其他查询。

Something like : 就像是 :

if ($user_type == 'admin') {
    echo "<tr><td>Type:</td><td><select id='type' >";
    echo "<option value='student' >student</option><option value='teacher'>teacher</option>";
    echo "</select></td></tr>";
    echo "<script> var method = 'val'</script>";
}
else 
{ 
    echo "<tr><td>Type:</td><td><div id='type'>" . $user_type . "</div></td></tr>";
    echo "<script> var method = 'text'</script>";
}

You can check it with the following simple code in javascript: 您可以使用javascript中的以下简单代码进行检查:

if(document.getElementById("type").tagName == "SELECT") {
   //Your code for admin
}
else
{
   //Your code if it is not admin
}

You can use $(selector).prop('tagName') to get the name of the tag and use .val() or .text() accordingly. 您可以使用$(selector).prop('tagName')获得标签的名称,并相应地使用.val().text() You can also use document.getElementById(id).nodeName in pure Javascript. 您也可以在纯Javascript中使用document.getElementById(id).nodeName

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="test"></select> <script> console.log($('#test').prop('tagName')); console.log(document.getElementById('test').nodeName); </script> 

You could also use a ternary operator to check if the value is undefined before assigning the value to a variable. 您还可以使用三元运算符在将值分配给变量之前检查该值是否未定义。

var value = $('#type').val()!==undefined?$('#type').val():$('#type').text();

Demo: 演示:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="createTag"> <option value="select">Select</option> <option value="div">Div</option> </select> <br/> <input type="text" placeholder="Enter value or text or element" id="val"/> <br/> <button onClick="createTag()">Create Tag</button> <button onClick="getValueOfType()">Get Value</button> <p/> <script> function createTag(){ var tag = $('#createTag').val(); $('#type').remove(); var elem = document.createElement(tag); var val = $('#val').val(); if(tag=="select"){ elem.innerHTML = "<option value='"+val+"' selected>"+val+"</option>"; } else { elem.textContent = val; } elem.id = "type"; document.body.appendChild(elem); } function getValueOfType(){ var elem = $('#type'); if(!elem.length){ alert("No element created yet."); } else { if(elem.prop('tagName')=="SELECT"){ alert(elem.val()); } else { alert(elem.text()); } } } </script> 

您可以使用简单优雅的三元条件获取文本或值:

var result = $("#type").val() !== undefined ? $("#type").val() : $("#type").text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM