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

如何使用Javascript检查HTML输入名称是否具有特定值?,javascript,php,html,forms,Javascript,Php,Html,Forms,在我的网站上,我正在用PHP处理一个HTML表单,但我需要用JavaScript来处理。使用JavaScript如何检查HTML输入名称是否具有特定值 基本上,我需要做与此等效的工作: <?php if (isset($_POST['reportsubmit'])) { $radio = $_POST['report']; if ($radio == 'customer') { $redirect = 'Click <a href="#customer

在我的网站上,我正在用PHP处理一个HTML表单,但我需要用JavaScript来处理。使用JavaScript如何检查HTML输入名称是否具有特定值

基本上,我需要做与此等效的工作:

<?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>
但是使用JavaScript。因此,我需要做的部分相当于这一部分:

<?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
}
?>
因此,基本上我需要知道如何读取HTML输入名称和值,并查看HTML输入名称是否具有特定值。

试试这个

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

var value = element.value;

var name = element.name;
第二种解决方案 正如我所看到的,您的代码有多个同名元素,所以要么将它们更改为具有不同的名称,要么出于设计目的使用下面提到的解决方案

或者给他们一个id并使用第一个解决方案

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


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

}

textid是textid还是应该替换那里的某个东西?它是你想要详细信息的元素的id。它可以是任何东西。因此,如果我想要它,它会是什么:我想看看一个名称,它是在多个输入上,是否有某个值。名称只能有一个值。我在看一个名字是否有一定的价值。如何查看名称是否具有特定值。顺便说一下,上面的代码完全不起作用。