php - 使用 javascript 时,我如何为选定的选项创建 if 语句

标签 php javascript

我是 javascript 的新手,但我正在尝试使用以下选项创建一个 if else 语句。

<select name="remote_server" class="required">
<option></option>
<option value="1" <?php if($remote=="1") echo 'selected="selected"'; ?>>Yes</option>
<option value="0" <?php if($remote=="0") echo 'selected="selected"'; ?>>No</option>
</select>

我想做类似的事情

if remote_server = 1 then 
    document.getElementByName("hostname").disabled = true;
else 
    nothing

编辑 1:

我现在正在尝试以下但没有成功

var e = document.getElementsByName("remote_server");
var strUser = e.options[e.selectedIndex].value;

if ( strUser == 0 ) {
    document.getElementsByName("ftp_hostname").disabled = true;
}

最佳答案

getElementsByName 返回一个元素数组,因此如果您想要第一个名为“remote_server”的元素,您应该使用 document.getElementsByName("remote_server")[0] .

同样,您需要使用 document.getElementsByName("ftp_hostname")[0].disabled

关于php - 使用 javascript 时,我如何为选定的选项创建 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998673/

相关文章:

php - 如何匹配mysql查询中的正则表达式?

URL 中的 php 标题

javascript - SimpleCart.js - 将运输信息添加到对象

php - 如何将Hive数据访问HTML页面

php - 分解数据数组

javascript - 无法在 IPython Notebook 中加载 jQuery DataTables 插件

javascript - 在 JavaScript 中提取给定 xpath 的数据/值

javascript - ZF : js is recognized but does not work in action views

javascript - Angularjs $compile(html)($scope) - 错误 : Converting circular structure to JSON

php - 使用 wordpress 多站点意外卸载了 AWS EC2 上的 mysql。需要一点指导