我有一个从数据库内容设置的按钮以及一个从同一数据库设置的选择选项。我希望能够单击按钮或从下拉列表中选择选项。
我已经尝试了很多在网上找到的不同选项。目前我可以提醒我要选择的文本,但无法更改下拉列表。
PHP file
$q = "SELECT CONCAT(user_first, ' ', user_last) AS name1, user_id as id from users ";
$r = @mysqli_query($conn, $q);
// Count the number of returned rows:
$num = mysqli_num_rows($r);
if ($num > 0) { // If it ran OK, display the records.
echo '<select class="select" name="name1" id="Seltherapist" autofocus tabindex="1">';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<option value="'.$row['id'].'">' . $row['name1'] . '</option>
';}
echo '</select>';
mysqli_free_result ($r);
} else {
echo '<p class="error">There are currently no registered users.</p>';
}
$num = mysqli_num_rows($r);
if ($num > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<button name="name22" value="'.$row['name'].'" id="'.$row['id'].'" class="btn">' . $row['name'] . '</button>
';}
JavaScript File
$('button[name="name22"]').click(function(){
alert($(this).attr("value"));
alert($(this).attr("id"));
event.preventDefault();
return false;
});
最佳答案
我不确定你是从哪里得到文本的,所以我们就称它为 yourText
。
let yourText = "someTextToMatch";
//By option text something like this.
$('#Seltherapist').find('option').each(function(i, v){
if ($(v).text() === yourText)
{
$(v).prop('selected', 'selected');
return false;
}
});
//by option value
$('#Seltherapist').val(yourText);
关于javascript - 使用按钮选择选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58204872/