[英]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;
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.