簡體   English   中英

如何使用Javascript檢查HTML輸入名稱是否具有特定值?

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

在我的網站上,我目前正在使用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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM