简体   繁体   English

HTML / Javascript从数据附加多个单词

[英]HTML / Javascript append multiple words from data

I am trying to append a text from a button to a modal window. 我正在尝试将按钮上的文本附加到模式窗口。

It works so far, but if the data contains more than 2 word, it just prints the first one what am I doing wrong? 到目前为止,它仍然有效,但是如果数据包含两个以上的单词,它将仅打印第一个单词,我在做什么错?

...
<button type="submit" class="observeSubmit deleteProject" data-title={{ $productValue->title }} name="removeProj">
...

Js function: js功能:

$('button[name="removeProj"]').on('click', function(e){
    var $form = $(this).closest('form'); // closest parent form
    e.preventDefault();

    $("#prodTitle").empty().append($(this).data('title') || '-');

    $('#confirm').modal({ backdrop: 'static', keyboard: false })
    $('#delete').click(function() {
        $form.trigger('submit'); // submit the form
    });
});

So if I click on the button a modal opens and shows the value inside $productValue->title . 因此,如果我单击按钮,则会打开一个模式并显示$productValue->title内的值。

If $productValue->title holds for example Jon Doe. 例如,如果$productValue->title成立,则为Jon Doe。

The window just shows Jon. 窗口仅显示Jon。

I am not sure if it is due to the data attribute, maybe it can just hold one value, or if it due to the javascript function. 我不确定这是否归因于data属性,也许它只能容纳一个值,还是归因于javascript函数。

您需要将自定义属性包装在引号中。

<button data-title="{{ $productValue->title }}"></button>
<button type="submit" class="observeSubmit deleteProject" data-title="{{ $productValue->title }}" name="removeProj">

现在,它为数据标题分配值“ Jon”,并将Doe视为单独的属性。

working fine here https://jsfiddle.net/ 在这里工作正常https://jsfiddle.net/

<form action="">
<button type="submit" class="observeSubmit deleteProject" data-title="test sdfg" name="removeProj">btntest</button>
</form>
<div id="prodTitle"></div>
$('button[name="removeProj"]').on('click', function(e){
    var form = $(this).closest('form'); // closest parent form
    debugger
    e.preventDefault();

    $("#prodTitle").empty().append($(this).data('title') || '-');

    $('#confirm').modal({ backdrop: 'static', keyboard: false })
    $('#delete').click(function() {
        $form.trigger('submit'); // submit the form
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM