简体   繁体   English

如何使用Javascript检查HTML输入名称是否具有特定值?

[英]How do I check if an HTML input name has a certain value with Javascript?

In my website I am processing an HTML form currently with PHP, but I need to do it with JavaScript. 在我的网站上,我目前正在使用PHP处理HTML表单,但是我需要使用JavaScript。 With JavaScript how do I check if an HTML input name has a certain value? 使用JavaScript,如何检查HTML输入名称是否具有特定值?

Basically I need to do the equivalent of this: 基本上,我需要这样做:

<?php
if (isset($_POST['reportsubmit'])) {
    $radio = $_POST['report'];
    if ($radio == 'customer') {
        $redirect = 'Click <a href="#customer">here</a> to continue on with the form';
    } else if ($radio == 'item') {
        $redirect = 'Click <a href="#item">here</a> to continue on with the form';
    } else if ($radio == 'department') {
        $redirect = 'Click <a href="#department">here</a> to continue on with the form';
    } else if ($radio == 'person') {
        $redirect = 'Click <a href="#person">here</a> to continue on with the form';
    }
} else if (isset($_POST['customersubmit'])) {
    //process form
    //redirect
} else if (isset($_POST['itemsubmit'])) {
    //process form
    //redirect
} else if (isset($_POST['departmentsubmit'])) {
    //process form
    //redirect
} else if (isset($_POST['personsubmit'])) {
    //process form
    //redirect
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Gordmart MIS Reports</title>
    <!--<link rel="stylesheet" href="../css/Main.css">-->
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>
<body>
    <div data-role="page" class="frame" id="report">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a report grouped by customers, items, sales departments, or sales people?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF;"], ENT_QUOTES, "utf-8"); ?>" method="post">
                <input type="radio" name="report" value="customer"><p>Customers</p>
                <input type="radio" name="report" value="item"><p>Items Sold</p>
                <input type="radio" name="report" value="department"><p>Sales Departments</p>
                <input type="radio" name="report" value="person"><p>Sales People</p>
                <input type="submit" name="reportsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="customer">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all customers, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="customer" value="all"><p>All</p>
                <input type="radio" name="customer" value="one"><p>One</p><br>
                <input type="submit" name="customersubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="item">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales items, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="item" value="all"><p>All</p>
                <input type="radio" name="item" value="one"><p>One</p><br>
                <input type="submit" name="itemsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="department">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales departments, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="department" value="all"><p>All</p>
                <input type="radio" name="department" value="one"><p>One</p><br>
                <input type="submit" name="departmentsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="person">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <h3>Would you like to view a cumulative report of all sales people, or a single report of just one?</h3>
            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'utf-8'); ?>" method="post">
                <input type="radio" name="person" value="all"><p>All</p>
                <input type="radio" name="person" value="one"><p>One</p><br>
                <input type="submit" name="personsubmit" value="Submit">
            </form>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
    <div data-role="page" class="frame" id="redirect">
        <div data-role="header">
            <?php include("Header.php");?>
        </div>
        <div data-role="main" id="main">
            <?php echo $redirect;?>
        </div>
        <div data-role="footer">
            <h1>Footer Text</h1>
        </div>
    </div>
</body>
</html>

But with JavaScript. 但是使用JavaScript。 So the part I would need to do the equivalent of is specifically this part: 因此,我需要做的等效部分就是这一部分:

<?php
if (isset($_POST['reportsubmit'])) {
    $radio = $_POST['report'];
    if ($radio == 'customer') {
        $redirect = 'Click <a href="#customer">here</a> to continue on with the form';
    } else if ($radio == 'item') {
        $redirect = 'Click <a href="#item">here</a> to continue on with the form';
    } else if ($radio == 'department') {
        $redirect = 'Click <a href="#department">here</a> to continue on with the form';
    } else if ($radio == 'person') {
        $redirect = 'Click <a href="#person">here</a> to continue on with the form';
    }
} else if (isset($_POST['customersubmit'])) {
    //process form
    //redirect
} else if (isset($_POST['itemsubmit'])) {
    //process form
    //redirect
} else if (isset($_POST['departmentsubmit'])) {
    //process form
    //redirect
} else if (isset($_POST['personsubmit'])) {
    //process form
    //redirect
}
?>

So basically I need to know how to read an HTML input name and value, and see if the HTML input name has a certain value. 因此,基本上我需要知道如何读取HTML输入名称和值,并查看HTML输入名称是否具有特定值。

Try this 尝试这个

var element = document.getElementById("textid");

var value = element.value;

var name = element.name;

Second solution 第二解决方案

As I can see your code has multiple element with same name so either change them to have different names and use the below mentioned solution (just for design sake). 如我所见,您的代码具有多个具有相同名称的元素,因此可以将它们更改为具有不同的名称,并使用下面提到的解决方案(仅出于设计目的)。

or give them an id and use the first solution. 或给他们一个ID并使用第一个解决方案。

var x=document.getElementsByName("report");


for(i=0;i<x.length;i++){
alert(x[i].name);

}

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

相关问题 如果某个值存在于某个数组索引处,我如何检查 JavaScript? - How do I check in JavaScript if a value exists at a certain array index? 我如何让我的 html 输入元素影响我的 javascript 类对象值(字符名称) - How do i get my html input element to influence my javascript class object value(character name) 如何检查 Javascript 中的值是否已更改? - How do I check if a value has changed in Javascript? 如何使用javascript检查和更改输入的值 - How do I check and change the value of an input with using javascript Javascript 如何检查输入值既不是空也不是数字? - Javascript How do I check that input value is neither empty or not a number? 如何使用JavaScript(或JQuery)检查输入是空的还是具有某个字符串? - How to use JavaScript (or JQuery) to check if an input is empty or has a certain string? 如何检查 html 元素是否包含带有 javascript 的特定文本 - how to check if an html element has certain text with javascript 如何使用javascript检查HTML输入文本框是否在某个数字以下? - How to check if HTML input textbox is below a certain number using javascript? 如何检查用户输入是否等于数组中的某个值? - How do I check if users input is equal to a certain value in my array? 使用Javascript检查radiobuttonlist是否具有特定值 - Check if radiobuttonlist has certain value using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM