Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法运行Javascript代码_Javascript_Jquery_Html - Fatal编程技术网

无法运行Javascript代码

无法运行Javascript代码,javascript,jquery,html,Javascript,Jquery,Html,我一直在使用javascript代码。我想向表中添加一个新行,然后可以删除它。我就是找不到我的错误 这是我的密码: <!DOCTYPE html> <html> <head> <script> $('.Add').click(function() { var row = $(this).closest('tr').clone(); row.find('input').val(''); $(this).closest('tr'

我一直在使用javascript代码。我想向表中添加一个新行,然后可以删除它。我就是找不到我的错误

这是我的密码:

<!DOCTYPE html>
<html>
<head>
<script>
$('.Add').click(function()
{
    var row = $(this).closest('tr').clone();
    row.find('input').val('');
    $(this).closest('tr').after(row);
    $('input[type="button"]',    row).removeClass('Add').addClass('RemoveRow').val('Remove item');
});

$('table').on('click', '.RemoveRow', function()
{
    $(this).closest('tr').remove();
});
</script>
</head>

<body>
<table>
    <tr>
        <td>Process:</td>
        <td>People required:</td>
        <td>Add/Remove Item</td>
    </tr>
    <tr>
        <td>
            <select name="process">
                <option value="1">BLD</option>
                <option value="2">KEY</option>
                <option value="3">ART</option>
                <option value="4">BG</option>
            </select>
        </td>
        <td><input type='text' ></td>
        <td><input type='button' class='Add' value='Add new item'></td>
    </tr>
</table>
</body>
</html>

$('.Add')。单击(函数()
{
var row=$(this.close('tr').clone();
行.find('input').val('');
$(this).最近('tr')。在(行)之后;
$('input[type=“button”]”,第行).removeClass('Add').addClass('removeow').val('Remove item');
});
$('table')。在('click','RemoveRow',function()上
{
$(this).closest('tr').remove();
});
过程:
所需人员:
添加/删除项目
BLD
钥匙
艺术
背景

您正在使用jQuery,但缺少jQuery引用

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

在函数上添加jQuery代码

工作代码:


$(函数(){
$('.Add')。单击(函数(){
var row=$(this.close('tr').clone();
行.find('input').val('');
$(this).最近('tr')。在(行)之后;
$('input[type=“button”]”,第行).removeClass('Add').addClass('removeow').val('Remove item');
});
$('table')。在('click','RemoveRow',function()上{
$(this).closest('tr').remove();
});
});
过程:
所需人员:
添加/删除项目
BLD
钥匙
艺术
背景
  • 将代码包装在
    文档中。就绪
    函数

  • 还包括jQuery库

  • 代码:


    使用Jquery的CDN/js文件,因为您在脚本中使用了
    $
    ,这将解决您的问题

    <!DOCTYPE html>
    <html>
    <head>
    // ------CDN of jquery
        <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script>
    $('.Add').click(function()
    {
        var row = $(this).closest('tr').clone();
        row.find('input').val('');
        $(this).closest('tr').after(row);
        $('input[type="button"]',    row).removeClass('Add').addClass('RemoveRow').val('Remove item');
    });
    
    $('table').on('click', '.RemoveRow', function()
    {
        $(this).closest('tr').remove();
    });
    </script>
    </head>
    
    <body>
    <table>
        <tr>
            <td>Process:</td>
            <td>People required:</td>
            <td>Add/Remove Item</td>
        </tr>
        <tr>
            <td>
                <select name="process">
                    <option value="1">BLD</option>
                    <option value="2">KEY</option>
                    <option value="3">ART</option>
                    <option value="4">BG</option>
                </select>
            </td>
            <td><input type='text' ></td>
            <td><input type='button' class='Add' value='Add new item'></td>
        </tr>
    </table>
    </body>
    </html>
    
    
    //----jquery的CDN
    $('.Add')。单击(函数()
    {
    var row=$(this.close('tr').clone();
    行.find('input').val('');
    $(this).最近('tr')。在(行)之后;
    $('input[type=“button”]”,第行).removeClass('Add').addClass('removeow').val('Remove item');
    });
    $('table')。在('click','RemoveRow',function()上
    {
    $(this).closest('tr').remove();
    });
    过程:
    所需人员:
    添加/删除项目
    BLD
    钥匙
    艺术
    背景
    

    您还可以在jsfiddle中查看代码,网址为

    您是否将
    jQuery
    包含在脚本中?查看consoleworking fine中的错误:@khorney.Idid@Riad没错,我也试着在那里运行它。它在工作,但在我的浏览器中不工作。
    <!DOCTYPE html>
    <html>
    <head>
    // ------CDN of jquery
        <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script>
    $('.Add').click(function()
    {
        var row = $(this).closest('tr').clone();
        row.find('input').val('');
        $(this).closest('tr').after(row);
        $('input[type="button"]',    row).removeClass('Add').addClass('RemoveRow').val('Remove item');
    });
    
    $('table').on('click', '.RemoveRow', function()
    {
        $(this).closest('tr').remove();
    });
    </script>
    </head>
    
    <body>
    <table>
        <tr>
            <td>Process:</td>
            <td>People required:</td>
            <td>Add/Remove Item</td>
        </tr>
        <tr>
            <td>
                <select name="process">
                    <option value="1">BLD</option>
                    <option value="2">KEY</option>
                    <option value="3">ART</option>
                    <option value="4">BG</option>
                </select>
            </td>
            <td><input type='text' ></td>
            <td><input type='button' class='Add' value='Add new item'></td>
        </tr>
    </table>
    </body>
    </html>