简体   繁体   English

如何在单击时获取目标的名称并将值分配给另一个变量?

[英]how to get the name of the target on click and assign the value to another variable?

So I have an empty variable 所以我有一个空变量

var pName= null;

Everytime I click on an image I want the variable 'pName' to change to the name of that image 每次单击图像时,我都希望变量“ pName”更改为该图像的名称。

$(document).ready(function(){
$('img').on('click', check)
});

function check(){
    pName == target.name.value;
    console.log('pName');
}

now I want to put the value of this variable into a hidden form (or any other way, if you suggest it), pick it up in php and change the value of the variable "$pName" (in php) to the value of the hidden form. 现在,我想将此变量的值设置为隐藏形式(或任何其他方式,如果您建议的话),在php中选择它,并将变量“ $ pName”(在php中)的值更改为隐藏的形式。

<label><input type="hidden" name="pNameChange" value=""></label>

and I want to do this on every page. 我想在每一页上都这样做。 My code doesn't seem to work. 我的代码似乎无效。

Edit 编辑

I have a variable that I use on every page $pName (php). 我在每个页面$ pName(php)上都有一个变量。 I want it to dynamically change to the name of the image you click. 我希望它动态更改为您单击的图像的名称。 I don't know if I'm doing it the right way. 我不知道我是否做对了。

Edit2 编辑2

This is my projects php file: 这是我的项目php文件:

<?php
$newRecord = null;
$pName = "kkk";
$db_connection = mysqli_connect('localhost','root','',"project_online_planner");
if (!$db_connection){
    die('Failed to connect to MySql:'.mysql_error());
}

//insert into database
if(isset($_POST['insertComments'])){
    include('connect-mysql.php');
    $username = $_POST['username'];
    $comment = $_POST['comment'];
    $sqlinsert = "INSERT INTO user_comments (username, comment, project) VALUES ('$username', '$comment', '$pName')";
        if (!mysqli_query($db_connection, $sqlinsert)){
            die('error inserting new record');
        }
        else{
            $newRecord = "1 record added";
        }//end nested statement

}

//text from database
$query="SELECT * FROM user_comments where project = '$pName' ";
$results = mysqli_query($db_connection,$query);
$intro=mysqli_fetch_assoc($results);

$query2="SELECT * FROM project where name = '$pName' ";
$results2 = mysqli_query($db_connection,$query2);
$intro2=mysqli_fetch_assoc($results2);


?>

Here I use $pName as the name of the project. 在这里,我使用$ pName作为项目的名称。 every image in my project has the name of the project they are in. I'm trying to dynamically change value of the variable $pName into what image is clicked. 我项目中的每个图像都有它们所在项目的名称。我试图将变量$ pName的值动态更改为单击的图像。

this is my all projects php file: 这是我所有的项目php文件:

<?php
$pName =
$db_connection = mysqli_connect('localhost','root','',"project_online_planner");
if (!$db_connection){
    die('Failed to connect to MySql:'.mysql_error());
}
$query="SELECT * FROM project ";
$results = mysqli_query($db_connection,$query);
$intro=mysqli_fetch_assoc($results);
?>
<!DOCTYPE HTML>
<html>
<head>
    <title>Project planner online</title>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="ppo.js"></script>
    <link rel="stylesheet" href="ppo.css"/>
</head>
<body>
<div id="bgNav">
    <nav id="nav">
        <a href="index.php">Home</a>
        <a class="rightNav" href="register.php">Register</a>
        <a class="rightNav" href="login.php">Log in</a>
    </nav>
</div>
<h2 class="titlePage">All projects</h2>
<div id="hugeTile">
    <?php

    while($row = mysqli_fetch_array($results))
    {
        $project = $row["project"];
        echo "<div class='allProjectsWrapper'>";
        echo nl2br("<div class='allProjectsTitle'>" . $row['name'] . "</div>");
        echo nl2br("<div class='project'>" ."<img name=\"$project\" width='240px' height='170px'  src='". $row['image'] ."'/>". "</div>");
        echo "</div>";
    }
    ?>
</div>
<form>
    <label><input type="hidden" name="pNameChange" value=""></label>
</form>

use like this, 这样使用

$('img').click(function(){
   pname= $(this).attr("name");
  });

First add an ID to the hidden field 首先将ID添加到隐藏字段

<label><input type="hidden" name="pNameChange" id="pNameChange" value=""></label>

Then you can pick up the image name like this 然后您可以像这样拾取图像名称

$('img').click(function()
{
    path = $(this).attr('src').split('\/');
    $img_name = path[path.length-1];
    $('#pNameChange').val($img_name); //Changing the pNameChange value
});

If you simply want to change the value of " pNameChange " after clicking the image, you don't need PHP. 如果您只想在单击图像后更改“ pNameChange ”的值,则不需要PHP。

Also, when you do 还有,当你做

pName == target.name.value;

It doesn' assign value, it uses comparison. 它不分配值,它使用比较。

= is used to assign values. =用于分配值。

== is used for comparison ==用于比较

Make sure you have your var globally defined, get the href attribute from the image and then set the input by its name, since you have no ID there: 确保您已全局定义var,从图像中获取href属性,然后通过其名称设置输入,因为那里没有ID:

<script type="text/javascript">
    var pName = '';

    $(document).ready(function(){
        $('img').on('click', function(){
            pName = $(this).attr('href');
            $("input[name=pNameChange]").val(pName);
        })
    }); 
</script>

this in the local scope will be the image element from the DOM. this在局部范围内将是从DOM图像元素。

You'll need to parse the href attr that you are sending in php because it will have the whole img url, unless you use a custom attribute or put the name in the image id or something like that: 您需要解析在php中发送的href属性,因为它将具有完整的img url,除非您使用自定义属性或将名称放在图像ID或类似的名称中:

<?php 
    $segments = explode('/', $pName);

    if(!empty($segments)) {
        $length = count($segments);
        $img_name = $segments[$length-1];
    }
?>

Just use this in your check() function: 只需在您的check()函数中使用它即可:

$("#pNameChange").val(pname);

and dont forget to add id "pNameChange" to your input 并且不要忘记在输入中添加id“ pNameChange”

<input type="hidden" id="pNameChange" name="pNameChange" value="">

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

相关问题 如何在散列中将变量值分配为变量名? - how to assign variable value as variable name in a hash? 如何在JavaScript中将变量的值分配为对象的名称 - How to assign a value of variable as name of an object in javascript 如何通过值将数组分配给另一个变量javascript - how to assign an array by value to another variable javascript 将变量值分配为变量名称 - Assign variable value as variable name 如何将一个变量的值分配给另一个变量的名称? [JavaScript,Vue.js] - How to assign value of one variable to name of another? [JavaScript, Vue.js] 将更改的函数分配给另一个变量,并获得具有相同名称的不同主体 - Assign a changed function to another variable and get different body with same name 如何将Javascript变量分配给另一个变量的相同值 - How to assign a Javascript variable to the same value of another variable 如何将变量值分配给另一个变量,而不是引用dojo javascript - how to assign a variable value to another variable instead of reference dojo javascript 当键名称本身是变量时,如何为数组键分配值 - How to assign the array key a value, when the key name is itself a variable 如何在JavaScript中将PHP文件名分配为变量值? - How do I assign a PHP file name as a variable value in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM